package org.jeecg.modules.monitoring.j_monitoring.job.screen;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.LicenseContractNew;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.LicenseSoftware;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.SoftwareModuleSetting;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.SoftwareSetting;
import org.jeecg.modules.monitoring.j_monitoring.mapper.LicenseContractNewMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.LicenseSoftwareMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.SoftwareModuleSettingMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.SoftwareSettingMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.ISoftwareModuleSettingService;
import org.jeecg.modules.monitoring.j_monitoring.service.ISoftwareSettingService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.util.Date;
import java.util.List;

/**
 * 同步专业软件基础
 * @Author: xiaoyq
 */

@Slf4j
public class SyncSoftwareDataJob implements Job {

    @Autowired
    private LicenseSoftwareMapper licenseSoftwareMapper;

    @Autowired
    private LicenseContractNewMapper licenseContractNewMapper;


    @Autowired
    private ISoftwareSettingService softwareSettingService;

    @Autowired
    private ISoftwareModuleSettingService softwareModuleSettingService;

    /***
     * 执行
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info(String.format("同步专业软件基础数据,时间:" + DateUtils.getTimestamp()));
        //同步软件许可信息
        //处理思路--先将statu状态设置为0状态--开始处理，新增设置为2，更新设置为1，未处理的还是保持为0，可以表示已经不存在，可以设置删除
        List<LicenseSoftware> licenseSoftwareList = licenseSoftwareMapper.selectList(null);
        UpdateWrapper<SoftwareSetting> softwareSettingUpdateWrapper = new UpdateWrapper<>();
        softwareSettingUpdateWrapper.lambda().set(SoftwareSetting::getStatus,"0");
        softwareSettingService.update(softwareSettingUpdateWrapper);
        LicenseSoftware fromSoftware;
        SoftwareSetting toSoftware;
        for (int i = 0; i < licenseSoftwareList.size(); i++) {
            fromSoftware = licenseSoftwareList.get(i);
            toSoftware = softwareSettingService.getById(fromSoftware.getId());
            if(toSoftware==null){
                toSoftware = SoftwareSetting.from(fromSoftware);
                toSoftware.setStatus("2");
                toSoftware.setIsDeleted("0");
                toSoftware.setIsshow(0);
                toSoftware.setSortNo(999d);
                toSoftware.setCreatetime(new Date());
            }
            else{
                toSoftware.copy(fromSoftware);
                toSoftware.setStatus("1");
                toSoftware.setIsDeleted("0");
            }
            softwareSettingService.saveOrUpdate(toSoftware);
        }

        softwareSettingUpdateWrapper.clear();
        softwareSettingUpdateWrapper.lambda().set(SoftwareSetting::getIsDeleted,"1");
        softwareSettingUpdateWrapper.lambda().eq(SoftwareSetting::getStatus,"0");
        softwareSettingService.update(softwareSettingUpdateWrapper);

        //模块处理
        List<LicenseContractNew> licenseContractNews = licenseContractNewMapper.selectList(null);
        UpdateWrapper<SoftwareModuleSetting> softwareModuleSettingUpdateWrapper = new UpdateWrapper<>();
        softwareModuleSettingUpdateWrapper.lambda().set(SoftwareModuleSetting::getStatus,"0");
        softwareModuleSettingService.update(softwareModuleSettingUpdateWrapper);
        LicenseContractNew fromModule;
        SoftwareModuleSetting toModule;
        for (int i = 0; i < licenseContractNews.size(); i++) {
            fromModule = licenseContractNews.get(i);
            toModule = softwareModuleSettingService.getById(fromModule.getId());
            if(toModule==null){
                toModule = SoftwareModuleSetting.from(fromModule);
                toModule.setStatus("2");
                toModule.setIsDeleted("0");
                toModule.setIsshow(0);
                toModule.setIskernel(0);
                toModule.setCreatetime(new Date());
            }
            else{
                toModule.copy(fromModule);
                toModule.setStatus("1");
                toModule.setIsDeleted("0");
            }
            softwareModuleSettingService.saveOrUpdate(toModule);
        }

        softwareModuleSettingUpdateWrapper.clear();
        softwareModuleSettingUpdateWrapper.lambda().set(SoftwareModuleSetting::getIsDeleted,"1");
        softwareModuleSettingUpdateWrapper.lambda().eq(SoftwareModuleSetting::getStatus,"0");
        softwareModuleSettingService.update(softwareModuleSettingUpdateWrapper);
    }

}
